set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO


If exists(select 1 from sysobjects where xtype = 'P' and name = 'Material_Save')
Begin
   Drop proc Material_Save
End

GO

CREATE     Proc [dbo].[Material_Save]
	(@Emp_Id smallint,
	 @Material_Id int OUT,
	 @Material_Nombre varchar(50),
	 @Material_Altura float,
	 @Material_Base float,
	 @Cod_Inventario varchar(50),
	 @Costo_Inventario float,
	 @Activo bit)
AS
Begin
	Set nocount on

	If Exists(Select 1 from Material Where Emp_Id = @Emp_Id and Material_Id = @Material_Id)
	Begin
		Update Material
		Set	Material_Nombre = @Material_Nombre,
			Material_Altura = @Material_Altura,
			Material_Base = @Material_Base,
			Cod_Inventario = @Cod_Inventario,
			Costo_Inventario = @Costo_Inventario,
			Activo = @Activo,
			Fec_Actualizacion = getdate()
		Where Emp_Id = @Emp_Id
		  and Material_Id = @Material_Id
	End
	Else
	Begin
		Select @Material_Id = isnull(max(Material_Id),0)+1
        From Material 
        Where Emp_Id = @Emp_Id

		Insert into Material
			(Emp_Id, Material_Id, Material_Nombre, Material_Altura, Material_Base,
			 Cod_Inventario, Costo_Inventario, Activo, Fec_Actualizacion)
		Values
			(@Emp_Id, @Material_Id, @Material_Nombre, @Material_Altura, @Material_Base, 
			 @Cod_Inventario, @Costo_Inventario, @Activo, getdate())
	End

	Set nocount on
End
 